package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.util.DBUtil;
import sun.security.pkcs11.Secmod;

public class UserServiceImpl implements UserService {

    @Override
    public User getUserByIdByPass(String userId, String password) {
        UserDao userDao = new UserDaoImpl();
        User user = null;
        try {
            //连接数据库
            DBUtil.getConnection();
            user=userDao.getUserByIdByPass(userId,password);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭Connection
            DBUtil.close();
        }
        return user;
    }

    @Override
    public int getUserById(String userId) {
        UserDao userDao = new UserDaoImpl();
        int result = 0;
        try {
            //连接数据库
            DBUtil.getConnection();
            result=userDao.getUserById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭Connection
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int saveUser(User user) {
        int result = 0;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            DBUtil.beginTransaction();
            result =dao.saveUser(user);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}
